<?php
/**
 * @notes:
 * @return
 * @author: admin
 * @date: 2024/9/11
 */
namespace addons\shopro\service\tokens;

use addons\shopro\service\Wallet;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\shopro\tokens\Config;
use app\admin\model\shopro\tokens\Pledgelog;
use app\admin\model\shopro\tokens\Record;
use app\admin\model\shopro\tokens\Pledge;
use app\admin\model\shopro\user\User;
use think\Db;
use think\Exception;
use think\exception\DbException;
use think\Paginator;

class PledgelogService
{

    protected $query = null;
    protected $format = null;

    public function __construct(\Closure $format = null, \think\db\Query $query = null)
    {
        $this->query = $query ?: new Pledgelog();
        $this->format = $format;
    }

    /***
     * 质押记录
     * @notes:
     * @param $user
     * @param $status
     * @return Paginator
     * @throws DbException
     * @author: 小谢
     * @date: 2024/9/11 13:47
     */
    public function getList($user,$status)
    {
        $map = [];
        $map[] = [
            'user_id', '=', $user->id
        ];
        if ($status) {
            $map[] = [
                'status', '=', $status
            ];
        }
        return $this->query->where($map)->paginate(request()->param('list_rows', 10));
    }


    public function finish()
    {
        $time = date('Y-m-d H:i:s');
        $logs=Pledgelog::where(['status'=>0])->where('endtime','<=',$time)->select();
        if(!empty($logs)){
            foreach ($logs as $item){
                $award = bcmul($item['amount'], $item['rate'] / 100, 2);
                $total =  bcadd($item['amount'], $award, 2);
                WalletService::change($item['user_id'],'tokens',$total,'token_pledge_finish');
                $item->status=1;
                $item->save();
            }
            return '执行成功';
        }
        return '没有需要执行的活动';
    }
}